
var domain = 'http://wheel.couchpotato.fun'
var wheelApi = {
    prize_list: '/index/wheel/get_prize_list',
    record_list: '/index/wheel/get_draw_record_list',
    topRecord_list: '/index/wheel/get_top_draw_record_list',
    drawPrize_list: '/index/wheel/draw?phone=13268327372'
}

function $ajax(methond, url, callback) {
    //1.实例化 XML对象
    var xhr = new XMLHttpRequest();
    //2.open(mthond,URL)
    xhr.open(methond, url)
    //3.添加侦听，侦听 xhr 对象状态值(readystate)的改变
    xhr.addEventListener("readystatechange", function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var res = JSON.parse(xhr.response);
            callback(res);
        }
    })
    xhr.send();
}

var topRecordInformation = document.querySelector("section .swiper-container .swiper-wrapper")   //顶部获奖输出
console.log(topRecordInformation);

var wheelPrizeName = document.querySelectorAll("section .content .left .lucky .luckyFirst  .content p")     //转盘奖品名字书写位置
var wheelPrizeImg = document.querySelectorAll("section .content .left .lucky .luckyFirst  .content img")    //转盘图片名字放置位置
var prizeTimeList = document.querySelector("section .content .right .Prizelist .prizeTime")     //获奖时间
var prizeList = document.querySelector("section .content .right .Prizelist .prize")             //获奖奖品 
var button = document.querySelector("section .content .left .lucky .button")                    //抽奖按钮
var luckyPrize = document.querySelector("section .content .left .lucky .luckyFirst")            //抽奖转盘
var prizeRe = { //奖品记录
    prize: {},
    id: 0
};
console.log(prizeTimeList);
console.log(prizeList);

//顶部获奖信息
function getPrizeTop(res) {
    console.log("dingbu", res)
    var sum = ``
    //var df = document.createDocumentFragment()
    for (var i = 0; i < 10; i++) {
        var protect = res.data[i].phone.substr(0, 3) + "****" + res.data[i].phone.substr(7)

        var temp = ` <div class="swiper-slide">
            <img src="./img/形状 68.png" alt=""></img> 
            恭喜 ${protect} 用户抽中 ${res.data[i].prize_name}</div> `
        sum += temp;
    }

    console.log(sum);
    topRecordInformation.innerHTML = sum;
    var mySwiper = new Swiper('.swiper-container', {
        direction: 'vertical', // 垂直切换选项
        loop: true, // 循环模式选项
        //initialSlide: 1,
        //speed:300,
        observer: true,       //开启观察者模式 ，当slide或  swiper变化时重新实例化swiper
        observeSlideChildren: true,
        autoplay: true,        //打开自动轮播


    })
}


//转盘奖品获取
function getWheelPrize(res) {
    for (let i = 0; i < 6; i++) {
        wheelPrizeName[i].innerText = `${res.data.prize_list[i].name}`
        wheelPrizeImg[i].src = res.data.prize_list[i].diagram;
    }
    console.log(res)
    prizeRe.prize = res;
}

//获取奖品列表函数
function getPrizeList(res) {
    for (var i = 0; i < res.data.draw_record_list.length; i++) {
        var li = document.createElement("li");
        li.innerText = `${res.data.draw_record_list[i].prize_name}`
        prizeList.appendChild(li)
        timeToDate(res, i)
    }
    //时间戳变为xxxx/xx/xx
    function timeToDate(res, i) {
        var date = new Date(res.data.draw_record_list[i].draw_time_stamp * 1000);
        var year = date.getFullYear();
        var mothon = date.getMonth() + 1;
        var day = date.getDate();
        var h = date.getHours();
        var m = date.getMinutes();
        if (m < 10) {
            m = "0" + m;
        }
        var li = document.createElement("li");
        li.innerText = `${year}.${mothon}.${day} ${h} :${m}`
        prizeTimeList.appendChild(li)
    }

}

//转盘偏移角度
var prize = [-30, -90, -150, -210, -270, -330]
var count = 0;
// 固定旋转角度
var ConstantAngle = 1800;
console.log(prize[0]);

//抽奖函数
function drawPrize(res) {
    var bingoId = res.data.bingo_prize_id;
    console.log(res.data.bingo_prize_id)
    if (bingoId > 6) bingoId = 6;
    var rotate = ConstantAngle * count + prize[bingoId - 1];
    luckyPrize.style[`transform`] = `rotateZ(${rotate}deg)`;
    luckyPrize.style[`transition`] = `all 1s`;
    prizeRe.id = bingoId;
    console.log(prizeRe.id);

}

//抽奖人信息函数
function drawPrizEvent() {
    count++;
    console.log(count)
    button.removeEventListener("click", drawPrizEvent);
    $ajax('GET', domain + wheelApi.drawPrize_list, drawPrize);
}


//  ajax传递数据
$ajax('GET', domain + wheelApi.topRecord_list, getPrizeTop);
$ajax('GET', domain + wheelApi.prize_list, getWheelPrize);
$ajax('GET', domain + wheelApi.record_list, getPrizeList);


//按钮点击事件
button.addEventListener("click", drawPrizEvent)
luckyPrize.addEventListener("transitionend", function () {
    button.addEventListener("click", drawPrizEvent)
    var prizeFinally = prizeRe.prize.data.prize_list[prizeRe.id - 1].name;
    console.log(prizeFinally);
    // (prizeFinally == "water" )? alert("谢谢惠顾"):alert(`恭喜你获得 ${prizeFinally}`)
})